package com.aurora.source.util.netutil;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.aurora.source.R;
import com.aurora.source.util.netutil.listener.ProgressCancelListener;

/**
 * @author: Lance
 * @description: ProgressDialog 进度 弹窗
 * @date: 2018/7/9 10:31
 */
public class ProgressDialogHandler extends Handler {

    String mDataLoading;

    public static final int SHOW_PROGRESS_DIALOG = 1;
    public static final int DISMISS_PROGRESS_DIALOG = 2;

    private ProgressDialog mProgressDialog;
    private Context mContext;
    private String mMessage;
    private boolean isCancelable;

    private ProgressCancelListener mProgressCancelListener;// 取消弹窗监听接口

    public ProgressDialogHandler(Context context, ProgressCancelListener mProgressCancelListener) {
        super();
        this.mContext = context;
        this.mProgressCancelListener = mProgressCancelListener;
    }

    public ProgressDialogHandler(Context context, ProgressCancelListener mProgressCancelListener, boolean cancelable) {
        super();
        this.mContext = context;
        this.mProgressCancelListener = mProgressCancelListener;
        this.isCancelable = cancelable;
    }

    public ProgressDialogHandler(Context context, ProgressCancelListener
            mProgressCancelListener, String message, boolean cancelable) {
        super();
        this.mContext = context;
        this.mMessage = message;
        this.mProgressCancelListener = mProgressCancelListener;
        this.isCancelable = cancelable;
    }

    private void initProgressDialog() {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(mContext);
            if (TextUtils.isEmpty(mMessage)) {
                mDataLoading = mContext.getString(R.string.data_loading);
                mProgressDialog.setMessage(mDataLoading);
            } else {
                mProgressDialog.setMessage(mMessage);
            }
            mProgressDialog.setCancelable(isCancelable);

            if (isCancelable) {
                mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        if (mProgressCancelListener != null) {
                            mProgressCancelListener.onCancelProgress();
                        }
                    }
                });
            }

            if (!mProgressDialog.isShowing()) {
                mProgressDialog.show();
            }
        }
    }

    private void dismissProgressDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case SHOW_PROGRESS_DIALOG:
                initProgressDialog();
                break;
            case DISMISS_PROGRESS_DIALOG:
                dismissProgressDialog();
                break;
        }
    }
}
